#!/bin/bash
#pMusic - external menu
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
SF='space-expand="false" space-fill="false"'
ST='space-expand="true" space-fill="true"'
read PRESS_EVENT < $WORKDIR/ALBUM_LIST_PRESS_EVENT
read SOURCE < $WORKDIR/trackinfo${MYPID}_album_source

template(){
	if [[ ! "$ALBUM_LIST" && "$*" = *global-option* && "$PRESS_EVENT" !=  *cdda:* ]]; then
		false #we accept global options for list in general as long as it is not a CD
	elif [[ "$SOURCE" != @(/*|CD audio) || `wc -l < $WORKDIR/trackinfo${MYPID}_album` -le 1 || ! "$ALBUM_LIST" ]]; then #no file
		SENSITIVE='<sensitive>false</sensitive>'
	elif [[ "$*" = *single-selection* && "$ALBUM_LIST" = *$'\n'* ]]; then
		SENSITIVE='<sensitive>false</sensitive>'
	elif [[ "$*" = @(*global-option*|*set_source*) && "$PRESS_EVENT" = *cdda:* ]]; then
		SENSITIVE='<sensitive>false</sensitive>'
	elif [[ "$*" = @(*add_tail*|*add_next*) && "$PRESS_EVENT" != *' /'* ]]; then
		SENSITIVE='<sensitive>false</sensitive>'
	fi

	echo '
	<eventbox above-child="true" visible-window="true">
	 <hbox spacing="7" border-width="3">
	  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
	  <text xalign="0" '$TOOLTIP' '$ST'>
	   '$SENSITIVE'
	   <label>'$1'</label>
	  </text>
	 </hbox>
	 <action signal="enter-notify-event">hide:'$2'</action>
	 <action signal="enter-notify-event">show:'$2'B</action>
	 <variable>'$2'</variable>
	</eventbox>
	<eventbox name="rightclick_menu" above-child="true" visible-window="true" visible="false">
	 <hbox spacing="7" border-width="3">
	  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
	  <text xalign="0" '$TOOLTIP' '$ST'>
	   '$SENSITIVE'
	   <label>'$1'</label>
	  </text>
	 </hbox>
	 <action signal="leave-notify-event">show:'$2'</action>
	 <action signal="leave-notify-event">hide:'$2'B</action>
	 <action signal="button-release-event">EXIT:'$2'</action>
	 <variable>'$2'B</variable>
	</eventbox>
	'
}

export gtkdialog_menu='
<window decorated="false" border-width="0" skip_taskbar_hint="true" resizable="false" modal="true" '$ST'>
 <eventbox name="rightclick_menu_frame" above-child="false">
  <vbox border-width="1">
   <eventbox>
    <vbox spacing="0" border-width="7">
     '"
     $(template " $(gettext 'Add Album to playqueue')" add_album icon_add_playqueue.svg global-option)
     <hseparator></hseparator>
     $(template " $(gettext 'Add to playqueue')" add_tail icon_add_playqueue.svg)
     $(template " $(gettext 'Add as next playing track(s)')" add_next icon_add_next.svg)
     $(template " $(gettext 'Play track (middle-button)')" play icon_play.svg single-selection)
     $(template " $(gettext '(Re)set track source')" set_source icon_refresh.svg single-selection)
     <hseparator height-request=\"5\"></hseparator>
     $(template " $(gettext 'Search for track')" search icon_pmusic_find.svg single-selection)
     <hseparator height-request=\"5\"></hseparator>
     $(template " $(gettext 'Send all info to masstagger')" tag go-forward global-option)
     $(template " $(gettext 'Edit album list in text-editor')" edit icon_edit.svg global-option)
     "'
    </vbox>
   </eventbox>
  </vbox>
 </eventbox>
 <action signal="focus-out-event">EXIT:exit</action>
</window>'

I=$IFS; IFS=""
for STATEMENTS in $($GTKDIALOG -p gtkdialog_menu -G +"$(($PTR_X_ROOT-5))"+"$(($PTR_Y_ROOT-5))"); do
	eval $STATEMENTS
done
IFS=$I
export PTR_BTN=""
case "$EXIT" in

play)
	$APPDIR/func -play_direct "PRESS_EVENT"
	;;

add_album)
	[ ! "$PRESS_EVENT" ] && continue
	cut -d'|' -f2-4 $WORKDIR/trackinfo${MYPID}_album > $WORKDIR/tmp.add
	$APPDIR/func_add -add $WORKDIR/tmp.add
	;;

add_tail)
	[ ! "$PRESS_EVENT" ] && continue
	[ "$(grep "^[0-9]" $WORKDIR/ALBUM_LIST_PRESS_EVENT)" ] && cut -c 21- $WORKDIR/ALBUM_LIST_PRESS_EVENT | sed "s/^/||/g" > $WORKDIR/tmp.add
	$APPDIR/func_add -add $WORKDIR/tmp.add
	;;

add_next)
	echo next > $WORKDIR/ADDMODE
	[ ! "$PRESS_EVENT" ] && continue
	[[ "$PRESS_EVENT" = [0-9]* ]] && tac $WORKDIR/ALBUM_LIST_PRESS_EVENT | cut -c 21- | sed "s/^/||/g" > $WORKDIR/tmp.add
	$APPDIR/func_add -add $WORKDIR/tmp.add
	;;

set_source)
	$APPDIR/box_open track $HOME "$PRESS_EVENT" #$1=mode $2=default_dir $3=window_title
	if [ -s $WORKDIR/open_output ]; then
		read NEW_PATH < $WORKDIR/open_output
		ACTIVE_PATH=${PRESS_EVENT#* }
		ACTIVE_NAME="$(grep -F "$PRESS_EVENT" "$SOURCE" | cut -d'|' -f3)"
		#now, let's replace
		grep -FB200 -m1 "|$PRESS_EVENT" "$SOURCE" | grep -vF "|$PRESS_EVENT" > $WORKDIR/tmp_trackinfo_album
		LENGTH=`grep -F Duration <<< "$(LC_ALL=C ffmpeg -i "$NEW_PATH" 2>&1)" | awk '{print $2}' | cut -d"." -f1 | cut -d":" -f2-`
		echo "|$LENGTH|$ACTIVE_NAME|$NEW_PATH|`date +%s%N` $NEW_PATH" >> $WORKDIR/tmp_trackinfo_album
		grep -FA200 "|$PRESS_EVENT" "$SOURCE" | grep -vF "|$PRESS_EVENT" >> $WORKDIR/tmp_trackinfo_album
		cp -f $WORKDIR/tmp_trackinfo_album "$SOURCE"
		mv $WORKDIR/tmp_trackinfo_album $WORKDIR/trackinfo${MYPID}_album
		echo true > $WORKDIR/UPDATE_TRACKINFO${MYPID} #update gui
	fi
	;;

search)
	SEARCH="$(grep -F "$PRESS_EVENT" $WORKDIR/trackinfo${MYPID}_album | cut -d'|' -f3)"
	. $APPDIR/func -search_refresh
	exit
	;;

tag)
	cut -d'|' -f4 $WORKDIR/trackinfo${MYPID}_album | grep -vE "http:|cdda:|^$" > $WORKDIR/id3tagger
	A=${ARTIST/${TXT_SEARCH}.../};	ARTIST=${A/${TXT_NO_WWW}/}
	A=${ALBUM/${TXT_SEARCH}.../};	ALBUM=${A/${TXT_NO_WWW}/}
	Y=${YEAR/${TXT_SEARCH}.../};	YEAR=${Y/${TXT_NO_WWW}/}
	#do not set up track-nr tagging if album is not complete and locally
	[ "`cut -d'|' -f4 $WORKDIR/trackinfo${MYPID}_album | grep -cE "http:|cdda:|^$"`" = 0 ] && echo "exe:printf \"%01d\" COUNT" > $WORKDIR/tmp_id3tagger_track || echo '' > $WORKDIR/tmp_id3tagger_track
	#run tagger
	. $APPDIR/func_id3tagger
	;;

edit)
	if [ ! "$SOURCE" ]; then
		TXT1="<b>$(gettext 'Album tracklist is not editable')</b>"
		. $APPDIR/box_ok
	fi
	$TEXT_EDITOR "$SOURCE" &
	;;

esac
